<?php

	require('./include/phpsqlajax_dbinfo.php');
	
	function parseToKML($htmlStr){
		$kmlStr=str_replace('&','&amp;',$htmlStr);
		return $kmlStr; 
	}
	
	 // Opens a connection to a MySQL server.
	$connection = mysql_connect ($server, $username, $password);
	if (!$connection) 
	{
	  die('Not connected : ' . mysql_error());
	}
	
	// Sets the active MySQL database.
	$db_selected = mysql_select_db($database, $connection);
	if (!$db_selected) 
	{
	  die ('Can\'t use db : ' . mysql_error());
	}
	
	 //Set result charset to UTF-8
	 mysql_query('SET NAMES "utf8"');
	 $query='SELECT eventcategory, COUNT(idEvents) AS amount 
	 		 FROM events, events_has_eventcategories, eventcategories 
	 		 WHERE idEvents = Events_idEvents 
	 		 AND EventCategories_idEventCategories = idEventCategories 
	 		 GROUP BY eventcategory';
	 
	 $result = mysql_query($query);
	 if (!$result) 
	 {
	  die('Invalid query: ' . mysql_error());
	 }
	 
	 // Creates an array of strings to hold the lines of the XML file.
	header('Content-type: text/xml');
	$xml = array('<?xml version="1.0" encoding="UTF-8"?>');
	$xml[] = '<statistics>';
	while ($row = @mysql_fetch_assoc($result)) 
	{
	  $xml[] = '<eventcategory id="'.$row['eventcategory'].'">';
	  $xml[] = '<amount>'.$row['amount'].'</amount>';
	  $xml[] = '</eventcategory>';
	}	 
	$xml[] = '</statistics>';
	
	$xmlOutput = join($xml);
	
	echo $xmlOutput;
	
	 
?>